<!-- S search -->
<app-search-bar
  [submitLoading]="tableParams.loading"
  [searchOptions]="searchOptions"
  labelFlex="80px"
  (onSearch)="queryResetData($event)"
  (onReset)="queryResetData($event)"
>
</app-search-bar>
<!-- E search -->

<nz-card class="m-t-4">
  <div nz-row nzJustify="space-between" nzAlign="bottom">
    <div nz-col class="opera-lf">
      <button
        *ngIf="permission.userPermission.has('storageCenter:purchase:add')"
        nz-button
        nzType="primary"
        class="m-r-8"
        routerLink="/storage-center/storage-purchase-edit"
        [queryParams]="{type: 0}"
      >新建</button>
    </div>
  </div>

  <div class="pagination-wrap-position p-t-10">
    <nz-table
      #basicTable
      nzSize="small"
      nzShowSizeChanger
      nzShowQuickJumper
      nzOuterBordered
      [nzScroll]="{ x: '1100px' }"
      [nzFrontPagination]="false"
      [nzLoadingDelay]="100"
      [nzLoading]="tableParams.loading"
      [nzData]="tableParams.tableData"
      [nzPageSizeOptions]="[5, 10, 20, 50, 100]"
      [nzTotal]="tableParams.total"
      [nzPageIndex]="tableParams.page"
      [nzPageSize]="tableParams.pageSize"
      [nzShowTotal]="totalTemplate"
      (nzPageIndexChange)="onPageIndexChange($event)"
      (nzPageSizeChange)="onPageSizeChange($event)"
    >
      <thead>
        <tr>
          <th nzAlign="center" nzWidth="200px" nzLeft>采购单号</th>
          <th nzAlign="center" nzWidth="120px">状态</th>
          <th nzAlign="center" nzWidth="220px">供应商</th>
          <th nzAlign="center" nzWidth="150px">采购内容</th>
          <th nzAlign="center" nzWidth="150px">冻结内容</th>
          <th nzAlign="center" nzWidth="150px">已入库</th>
          <th nzAlign="center" nzWidth="170px">约定交货日期</th>
          <th nzAlign="center" nzWidth="180px">备注</th>
          <th nzAlign="center" nzWidth="170px">结案时间</th>
          <th nzAlign="center" nzWidth="170px">新建时间</th>
          <th nzAlign="center" nzWidth="280px" nzRight>操作</th>
        </tr>
      </thead>

      <tbody>
        <tr *ngFor="let data of basicTable.data; let index = index;">
          <!-- 采购单号 -->
          <td nzAlign="center" nzLeft>
            <ng-container *ngIf="data.purchaseNumber; else elseCgdTemplate">
              <a
                style="color: #409EFF;"
                routerLink="/storage-center/storage-purchase-details"
                [queryParams]="{id: data.id}"
              >
                {{ data.purchaseNumber || '-' }}
              </a>
            </ng-container>
            <ng-template #elseCgdTemplate>
              -
            </ng-template>
          </td>
          <!-- 状态 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.state | storagePurchaseState: 'color'}">
              {{ data.state | storagePurchaseState: 'label' }}
            </span>
          </td>
          <!-- 供应商 -->
          <td nzAlign="center">
            {{ filterText(supplierOptions, data.supId, 'id') }}
          </td>
          <!-- 采购内容 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.applyContent === data.storedContent ? '#909399' : '#FF9900'}">
              {{ data.applyContent || '-' }}
            </span>
          </td>
          <!-- 冻结内容 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.applyContent === data.storedContent ? '#909399' : '#FF9900'}">
              {{ data.frozenContent || '-' }}
            </span>
          </td>
          <!-- 已入库 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.applyContent === data.storedContent ? '#909399' : '#FF9900'}">
              {{ data.storedContent || '-' }}
            </span>
          </td>
          <!-- 约定交货日期 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': filterTimeColor(data)}">
              {{ data.deliveryTime || '-' }}
            </span>
          </td>
          <!-- 备注 -->
          <td nzAlign="center">{{ data.remark || '-' }}</td>
          <!-- 结案时间 -->
          <td nzAlign="center">{{ data.closingTime || '-' }}</td>
          <!-- 新建时间 -->
          <td nzAlign="center">{{ data.createTime || '-' }}</td>
          <!-- 操作 -->
          <td nzAlign="center" nzRight>
            <button
              nz-button
              nzType="link"
              nzSize="small"
              routerLink="/storage-center/storage-purchase-details"
              [queryParams]="{id: data.id}"
            >查看</button>

            <button
              *ngIf="permission.userPermission.has('storageCenter:purchase:add') && data.state === 0"
              nz-button
              nzType="link"
              nzSize="small"
              routerLink="/storage-center/storage-purchase-edit"
              [queryParams]="{
                type: 1,
                id: data.id,
                purchaseNumber: data.purchaseNumber
              }"
            >编辑</button>

            <button
              *ngIf="permission.userPermission.has('storageCenter:purchase:del') && data.state === 0"
              nz-button
              nzType="link"
              nzSize="small"
              nzDanger
              nz-popconfirm
              nzPopconfirmTitle="该操作不可逆，确认删除?"
              (nzOnConfirm)="deleteItem(data)"
            >删除</button>

            <button
              *ngIf="permission.userPermission.has('storageCenter:purchase:add') && data.state === 0"
              nz-button
              nzType="link"
              nzSize="small"
              nz-popconfirm
              nzPopconfirmTitle="确认提交审核?"
              (nzOnConfirm)="submitReview(data, 1)"
            >提交审核</button>

            <button
              *ngIf="permission.userPermission.has('storageCenter:purchase:examine') && data.state === 1"
              nz-button
              nzType="link"
              nzSize="small"
              (click)="showExamineModal(data)"
            >审核</button>

            <button
              *ngIf="permission.userPermission.has('storageCenter:purchase:examine') && data.state === 2"
              nz-button
              nzType="link"
              nzSize="small"
              nz-popconfirm
              nzPopconfirmTitle="确认将单据打回草稿状态?"
              (nzOnConfirm)="submitReview(data, 6)"
            >反审核</button>

            <button
              *ngIf="permission.userPermission.has('storageCenter:put:add') && data.state === 2"
              nz-button
              nzType="link"
              nzSize="small"
              nzDanger
              routerLink="/storage-center/storage-warehous-editcg"
              [queryParams]="{purchaseNumber: data.purchaseNumber, type: 0}"
            >生成入库单</button>

            <button
              *ngIf="permission.userPermission.has('storageCenter:purchase:ja') && data.state === 2"
              nz-button
              nzType="link"
              nzSize="small"
              (click)="closeCase(data)"
            >结案</button>

            <!-- <button
              nz-button
              nzType="link"
              nzSize="small"
            >导出</button> -->

            <button
              nz-button
              nzType="link"
              nzSize="small"
              (click)="showFollowModal(data)"
            >跟进</button>
          </td>
        </tr>
      </tbody>
    </nz-table>

    <!-- 分页template -->
    <ng-template #totalTemplate let-total>共有 {{ total }} 条</ng-template>
  </div>
</nz-card>

<!-- S 审核 Modal -->
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [(nzVisible)]="isExamineVisible"
  [nzTitle]="'审核'"
  (nzOnCancel)="isExamineVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <nz-textarea-count [nzMaxCharacterCount]="150">
        <textarea
          rows="4"
          nz-input
          maxlength="150"
          placeholder="请输入审核意见"
          [(ngModel)]="auditOpinion"
        ></textarea>
      </nz-textarea-count>
    </div>

    <div class="custom-model-foot">
      <div class="custom-foot-cont">
        <button nz-button nzType="primary" (click)="subExamine(2)">审核通过</button>
        <button nz-button nzType="primary" nzDanger (click)="subExamine(5)">审核不通过</button>
      </div>
    </div>
  </div>
</nz-modal>
<!-- E 审核 Modal -->

<!-- S 跟进Component -->
<app-follow-up
  #followUpRef
  [initParams]="initParams"
  [ohterParams]="{ type: 0, sourceType: 99 }"
></app-follow-up>
<!-- E 跟进Component -->